<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<script language="javascript" type="text/javascript">
	
	function checkCookie(){
		if(!(document.cookie || navigator.cookieEnabled)){
			return false;
		}
		return true;
	}
	
	function handleEnter(field, event) {
		if (event.keyCode == 13) { //回车
			if (field.name == "username") {
				if ($.trim($('#username').val()) != '') {
					$('#userpass').focus();
				}
				return false;
			}
			if (field.name == "userpass") {
				if ($.trim($('#userpass').val()) != '' && $('#userpass').val().length >= 6) {
				  if(typeof(wtype) != 'undefined' && (wtype == 1 || wtype==2)){
				    logInvited();
				  }else{
					login();
				  }
				}
			}
			return true;
		}
	}
	
	function login() {
		$('#loginmsg').html('&nbsp;');
	    if( !checkCookie() ){
	    	$('#loginmsg').text("请启动cookie");
	    	return;
	    }
		if ($("#username").val() == "") {
			$('#loginmsg').text('用户名不能为空');
			return;
		}
		if ($("#userpass").val() == "") {
			$('#loginmsg').text('用户密码不能为空');
			return;
		}
		
		$.ajax({
			type:"POST",
			url:"cts/login.action",
			async: false,
			data:$("#loginForm").serialize(),
			success:function (data){
				var arr=data.split('|');
				if(arr[0] == 1){
	       			var identity=arr[1];
					var defaultUrl = "${pageContext.request.contextPath}/cts/index";
				    window.location.href = defaultUrl;
	       		}else if(arr[0] == -1){
	       			$('#loginmsg').text('用户名或密码错');
	       		}else if(arr[0] == -2){
	       			$('#loginmsg').text('该用户已被禁用');
	       		}else if(arr[0] == -3){
	       			window.location.href='${pageContext.request.contextPath}/cts/findCtsInfo';
	       		}else{
	       		 	window.location.href = "${pageContext.request.contextPath}/cts/index";
	       		}
			}
		});
		
	}

	function closeFindPwdDiv(){
		$('#open_div_findpwd').fadeOut();
	}
	
	function checkCookie(){
		if(!(document.cookie || navigator.cookieEnabled)){
			return false;
		}
		return true;
	}
	function showPwdTxt(id){
		$('#'+id+'2').hide();
		$('#'+id).show().get(0).focus();
	}
	function hidePwdTxt(id){
		var txt = $('#'+id)
		var txt2 = $('#'+id+'2')
		if(txt.val() == '' || txt.val() == txt2.val()){
			txt.hide();
			txt2.show();
			txt.val('');
		}
	}
	
	var stepIndex = 1;
	function toLogPlay(){
	  if(stepIndex>1){
	    $("#tabPlayShow").animate({"margin-left":"+=380px"} , "fast");
	    stepIndex--;
	  }
	  
	  if(stepIndex == 1){
	     $("#loginX").find("div[class='popdiv_handle']").text("用户登录");
	     var userName = $("#username").val();
	     var reguserName = $("#regUsername").val();
	     if(reguserName != '' && userName ==''){
	       $("#regUsername").val('');
	       $("#regUsername2").val('');
	       $("#regEnUserpass2,#regEnUserpass,#regUserpass2,#regUserpass").val('');
	       if(isEmail(reguserName)){
		       $("#username").val(reguserName);
		       $("#username2").val(reguserName);
	       }
	     }
	  }
	}
	
	function toRegPlay(){
	  if(stepIndex<3){
	    $("#tabPlayShow").animate({"margin-left":"-=380px"} , "fast");
	    stepIndex++;
	  }
	  
	  if(stepIndex == 2){
	     $("#loginX").find("div[class='popdiv_handle']").text("用户注册");
	     var userName = $("#username").val();
	     var reguserName = $("#regUsername").val();
	     
	     if(reguserName == '' && userName!=''){
	       $("#username").val('');
	       $("#username2").val('');
	       $("#userpass").val('');
	       $("#userpass2").val('');
	       if(isEmail(userName)){
		       $("#regUsername").val(userName);
		       $("#regUsername2").val(userName);
	       }
	     }
	  }
	}
	
</script>



<style style="text/css">
	.login_input{
	     margin:0;
	     width:163px;
	     height:40px;
	     line-height:40px;
	     border:none;
	}
		
	.login_txt{
	    width:163px;
	    height:50px;
	    line-height:50px;
	    background: #ffff; 
	    color: #687da1;
	    border:none;
	}
	
	.a_login,.a_login:visited,.a_login:hover{background:url(${pageContext.request.contextPath}/cts/images/login_btn.png) no-repeat center; width: 82px; height: 40px; border: 0;
		color: #cfcfcf; text-decoration: none; line-height: 23px; display: inline-block; margin: 0 10px 0 10px; line-height: 40px;}
	.a_login:hover{color: white;}
	.a_login:active{background:url(${pageContext.request.contextPath}/cts/images/login_active.png) no-repeat center; width: 82px; height: 40px; border: 0;color: white;}
	.a_reg,.a_reg:visited,.a_reg:hover{background:url(${pageContext.request.contextPath}/cts/images/reg_btn.png) no-repeat center; width: 82px; height: 40px; border: 0;
		 color: #cfcfcf; text-decoration: none; line-height: 23px; display: inline-block; margin: 0 10px 0 10px; line-height: 40px;}
	.a_reg:hover{color: white;}
	a.getpwd{font-weight: normal; color: #795e42; text-decoration: none;}
	a.getpwd:hover{text-decoration: underline;}
	
	
	.ruote1{
         line-height:20px;
         _padding:5px; 
	     -moz-border-top-right-radius: 5px;
	     -webkit-border-top-right-radius: 5px; 
	      border-top-left-radius: 5px;
	      border-top-right-radius: 5px;
	      
	     -webkit-border-top-left-radius: 5px;
	     -moz-border-top-left-radius: 5px;
	     
	     -moz-border-bottom-right-radius: 5px;
	     -webkit-border-bottom-right-radius: 5px; 
	      border-bottom-left-radius: 5px;
	      border-bottom-right-radius: 5px;
	      
	     -webkit-border-bottom-left-radius: 5px;
	     -moz-border-bottom-left-radius: 5px;
    }
    
    .tda{}
    .tda a{
      text-decoration:none;
      fonts-size:18px;
      width:124px;
      height:42px;
      line-height:42px;
      _height:35px;
      _line-height:35px;
      background-color:#81991a;
      float:left;
    }
    
    .aircontent{
      width:263px;
      _width:270px;
      height:102px;
      margin-left:60px;
      _margin-left:30px;
      overflow:hidden;
      border:1px solid #ccc;
    }
    
    .aircontent .bgb1{
      width:80px;height:50px;line-height:50px;
      border-bottom:1px solid #ccc;
    } 
    
    .aircontent .bgb2{
       height:50px;line-height:50px;
    }
    #tabPlayShow table{
      font-family:微软雅黑, 宋体;
      font-size:14px;font-weight:normal;
      color:gray;
    }
    
    .userName1{
       height:40px;
       line-height:40px;
       width:99%;
       float:left;
       border:none;
       color:#687da1;
       font-size:18px;
    }
    
    .userPass1{
      height:40px;line-height:40px;width:99%;float:left;border:none;font-size:18px;
    }
    
    .bgc1{
       background:url("${pageContext.request.contextPath}/cts/images/nextTo.gif") #fff no-repeat 111px 16px; 
       _background:url("${pageContext.request.contextPath}/cts/images/nextTo.gif") #fff no-repeat 119px 16px; 
    }
    
    .bgc2{
       background:url("${pageContext.request.contextPath}/cts/images/prevTo.gif") #fff no-repeat 10px 17px; 
       _background:url("${pageContext.request.contextPath}/cts/images/prevTo.gif") #fff no-repeat 10px 16px; 
    }
</style>


 	<table align="center" id="nostyletab" style="width:100%; text-align:center;" cellspacing="0">
 		<tr>
 		   <td  colspan="2">
 		     <div style="width:380px;_height:260px;overflow:hidden;">
			     <table cellpadding="0" cellspacing="0" border="0" id="tabPlayShow">
			        <tr>
			          <td valign="top">
			             <div style="width:370px;">
				             <table style="width:100%;">
				                <tr>
			 		     	      <td style="text-align: center;height:27xp;line-height:27px;_height:10px;_line-height:10px;color:red;font-size:12px;font-weight:normal;"><span id="loginmsg">&nbsp;</span></td>
			 		     	    </tr>
				             </table>
				             <form method="post" id="loginForm" name="loginForm">
						              <div id="userLogContent" class="ruote1 aircontent">
							 		     	 <table cellpadding="0" cellspacing="0" border="0">
								 		        <tr>
								 		          <td class="bgb1">E-mail：</td>
								 		          <td class="bgb1" style="width:183px;">
								 		             <input  tabindex="11" type="text" name="username2" id="username2" onfocus="showPwdTxt('username')" value="" class="userName1"/>
							 				         <input  tabindex="11" type="text" name="username"  id="username" onblur="hidePwdTxt('username')" onkeypress="handleEnter(this, event);" value="" style="display: none;" class="userName1"/>
								 		          </td>
								 		        </tr>
								 		        <tr>
								 		          <td class="bgb2" style="text-align:left;padding-left:8px;">密码：</td>
								 		          <td class="bgb2">
								 		             <input tabindex="12" type="text" name="userpass2" id="userpass2" onfocus="showPwdTxt('userpass')" value="" class="userPass1"/>
							 				         <input tabindex="12" type="password" name="userpass" id="userpass" onblur="hidePwdTxt('userpass')" onkeypress="handleEnter(this, event);" style="display: none;" class="userPass1"/>
								 		          </td>
								 		        </tr>
								 		     </table>
							 		  </div>
							 		  <table style="width:100%;">
					 		        	<tr>
									 	  <td id="loginInvited1" colspan="2" style="padding-top:14px;_padding-top:6px;" class="tda">
								 				<a tabindex="13" class="ruote1" href="javascript:login();" style="margin-left:58px;_margin-left:56px;background-color:#e1e1e1;color:gray;font-size:16px;">登录</a>
								 				<a tabindex="13" class="ruote1 bgc1" href="javascript:toRegPlay()"  style="margin-left:15px;color:#fff;font-size:16px;">
								 				   注册
								 				</a>
									 	  </td>
									 	</tr>
									 	<tr>
									 	  <td id="loginInvited2"  colspan="2" style="padding-top:14px;_padding-top:6px;display:none;" class="tda">
								 				<a tabindex="13" class="ruote1" href="javascript:logInvited();" style="margin-left:120px;_margin-left:56px;background-color:#e1e1e1;color:gray;font-size:16px;">登录</a>
									 	  </td>
									 	</tr>
									 	<tr><td colspan="2">&nbsp;</td></tr>
								 		<tr>
								 			<td align="left">
								 				&nbsp;<a class="getpwd" href="javascript:findPassWord();" style="color:#71573c;font-size:12px;"> 忘记密码?</a>
								 			</td>
								 			<td align="right" style="color: #859325;font-size:12px;font-weight:normal;">
								 				两周内免登录 <input type="checkbox" name="loginFlag" id="loginFlag" value="1" style="vertical-align: middle; border: 1px solid transparent;"/>
								 			</td>
								 		</tr>
								 	 </table>
						 	  </form>
			             </div>
			          </td>
			          <td style="padding-top:20px;" valign="top">
			             <div style="width:370px;">
				              <div id="userLogContent"  class="ruote1 aircontent" style="height:152px;">
					 		     <form id="regUserForm" name="regUserForm" method="post">
						 		      <table cellpadding="0" cellspacing="0" border="0">
							 		        <tr>
							 		          <td class="bgb1" style="width:80px;text-align:left;padding-left:8px;">E-mail：</td>
							 		          <td class="bgb1" style="width:187px;">
							 		             <input  tabindex="14" type="text" name="regUsername2" id="regUsername2" onfocus="showPwdTxt('regUsername')" value=""  class="userName1"/>
						 				         <input  tabindex="14" type="text" name="user.userName"  id="regUsername" onblur="hidePwdTxt('regUsername')" onkeypress="handleEnter(this, event);" value="" style="display: none;"  class="userName1"/>
							 		          </td>
							 		        </tr>
							 		        <tr>
							 		          <td class="bgb1" style="text-align:left;padding-left:8px;">密码：</td>
							 		          <td class="bgb1">
							 		             <input tabindex="15" type="text" name="regUserpass2" id="regUserpass2" onfocus="showPwdTxt('regUserpass')" value=""  class="userPass1"/>
						 				         <input tabindex="15" type="password" name="regUserpass" id="regUserpass" onblur="hidePwdTxt('regUserpass')" onkeypress="handleEnter(this, event);" style="display: none;" class="userPass1"/>
							 		          </td>
							 		        </tr>
							 		        <tr>
							 		          <td class="bgb2" style="text-align:left;padding-left:8px;">确认密码：</td>
							 		          <td class="bgb2">
							 		             <input tabindex="16" type="text" name="regEnUserpass2" id="regEnUserpass2" onfocus="showPwdTxt('regEnUserpass')" value="" class="userPass1" />
						 				         <input tabindex="16" type="password" name="user.userPass" id="regEnUserpass" onblur="hidePwdTxt('regEnUserpass')" onkeypress="handleEnter(this, event);" style="display: none;" class="userPass1"/>
							 		          </td>
							 		        </tr>
							 		  </table>
					 		     </form>
					 		  </div>
					 		  <table>
			 		        	<tr>
							 	  <td id="regTool1" colspan="2" style="padding-top:14px;" class="tda">
						 				<a tabindex="13" id="regReturnLogin" class="ruote1 bgc2" href="javascript:toLogPlay()" style="margin-left:58px;_margin-left:33px;background-color:#e1e1e1;color:gray;font-size:16px;font-weight:normal;">
						 				 返回登录
						 				</a>
						 				<a tabindex="14" id="regToReg" class="ruote1 bgc1" href="javascript:register_saveUser()"  style="margin-left:15px;color:#fff;font-size:16px;font-weight:normal;">
						 				  提交注册
						 				</a>
							 	  </td>
							 	  <td id="regTool2" colspan="2" style="padding-top:14px;display:none;text-align:cetner;" class="tda">
							 	     <a tabindex="14" id="regToReg" class="ruote1 bgc1" href="javascript:regInvited()"  style="margin-left:130px;color:#fff;font-size:16px;font-weight:normal;">
						 				  提交注册
						 			 </a>
							 	  </td>
							 	</tr>
							 	<tr>
							 	   <td colspan="2" style="height:52px;">&nbsp;</td>
							 	</tr>
						 	 </table>
			             </div>
			          </td>
			          
			          <td valign="top">
			            <div style="width:370px;"></div>
			              <table cellpadding="0" cellspacing="0" style="width:100%;text-align:center;font-size:18px;color:#3f5b87;font-weight:normal;margin-top:30px;margin-left:25px;_margin-left:10px;">
			                 <tr>
			                   <td style="height:60px;">&nbsp;</td>
			                 </tr>
			                 <tr>
			                    <td style="color:#90aa31;">恭喜您注册成功！</td>
			                 </tr>
			                 <!-- 
			                 <tr>
			                    <td style="padding-top:30px;"><span style="font-size:14px;">登录后请点击页面左上角</span>"<img src="${pageContext.request.contextPath}/cts/images/addToCA.gif"/>"</td>
			                 </tr>
			                 <tr>
			                    <td  style="padding-bottom:30px;">
			                      <span style="color:#90aa31;">创建</span>
			                      <span style="font-size:14px;">或</span>
			                      <span style="color:#90aa31;">加入</span>
			                      <span style="font-size:14px;">新的货物跟综系统</span>
			                    </td>
			                 </tr>
			                  -->
			                 <tr>
			                     <td style="height:30px;">&nbsp;</td>
			                 </tr>
			                 <tr>
			                    <td  class="tda">
			                      <a tabindex="13" class="ruote1" href="javascript:regToLogin();" style="margin-left:120px;_margin-left:100px;color:#fff;">登录</a>
			                    </td>
			                 </tr>
			              </table>
			          </td>
			        </tr>
			     </table>
			     
			     
 		     </div>
		  </td>
 		</tr>
 	</table>
 
 
 <script type="text/javascript">
        function passwordMsg(passTxt , passTxt2){
	        var mb = {};
	        mb.isOk = true;
	        mb.msg = true;
	        if(passTxt == null || passTxt == ''){
	           mb.msg = '密码不能为空';
	           mb.isOk = false;
	        }else if(passTxt.length <6){
	           mb.msg = '密码最少长度为6位';
	           mb.isOk = false;
	        }else if(passTxt.length >25){
	           mb.msg = '密码最长长度为25位';
	           mb.isOk = false;
	        }else{
		       if(typeof(passTxt2)!='undefined'){
		           if(passTxt!=passTxt2){
		             mb.msg = '二次输入的密码不一致';
		             mb.isOk = false;
		           }
		       }
	        }
	        
	        
	        return mb;
	     }
     
        function register_saveUser(){
           var userName = $("#regUsername").val();
           var userPass = $("#regUserpass").val();
           var reEnterPwd = $("#regEnUserpass").val();
           
           if(userName == ''){
              alert("用户名不能为空");
              return;
           }
           
            if(userName!="" && !isEmail(userName)){
        	  alert("用户名必须为您的email地址");
        	  return;
           }
           
           
           var vailPass = passwordMsg(userPass , reEnterPwd);
           if(vailPass.isOk == false){
              alert(vailPass.msg);
              return;
           }
           
           var url = "cts/findUserByType?username="+userName+"&type=1&returnUid=0&sta=0,1";
           $.post(url , "" , function(data){
              if(data!=0){
                   alert("此帐号已存在");
              }else{
		           var param = $("#regUserForm").serialize();
		           $.post("cts/saveUser.action" , param , function(data){
		               if(data == 1 || data == 2){
		                 alert("用户注册成功");
		                 toRegPlay();
		               }else{
		                 alert("用户注册失败");
		               }
		           });
              }
           });
           
        }
        
        function regToLogin(){
             var userName = $("#regUsername").val();
             var userPass = $("#regUserpass").val();
             
             $("#username").val(userName);
             $("#userpass").val(userPass);
             
             login();
        }
        
        
        function isEmail(str) {
			if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
				return true;
			} else {
				return false;
		    }
		}
</script>


<script type="text/javascript">
   function invitedFRAM(){
     if(wtype == 1){
        toRegPlay();
        $("#regUserForm").find("input[id='regUsername']").val(userName_0);
        $("#regUserForm").find("input[id='regUsername2']").val(userName_0);
        $("#regUserForm").find("input[id='regUsername']").attr({"readonly":"readonly"});
        $("#regUserForm").find("input[id='regUsername2']").attr({"readonly":"readonly"});
        $("#regTool1").hide();
        $("#regTool2").show();
     }else if(wtype == 2){
	    $("#loginForm").find("input[id='username']").val(userName_0);
	    $("#loginForm").find("input[id='username2']").val(userName_0);
	    $("#loginForm").find("input[id='username']").attr({"readonly":"readonly"});
	    $("#loginInvited1").hide();
        $("#loginInvited2").show();
     }
     
   }
   
   function regInvited(){
	   toRegPlay();
	   return;
      var userName = $("#regUsername").val();
      var userPass = $("#regUserpass").val();
      var reEnterPwd = $("#regEnUserpass").val();
      
      if(userName == ''){
         alert("用户名不能为空");
         return;
      }
      
      if(userName!="" && !isEmail(userName)){
   	    alert("用户名必须为您的email地址");
   	    return;
      }
      
      
      var vailPass = passwordMsg(userPass , reEnterPwd);
      if(vailPass.isOk == false){
         alert(vailPass.msg);
         return;
      }
      
      var param = "?";
      param += "userId="+userId_0;
      param += "&employeeId="+employeeId_0;
      param += "&inviteCode="+invitedCode_0;
      param += "&pwd="+userPass;
      
      $.post("cts/angainAdd" + param , "" , function(data){
           if(data == -1){
              alert("邀请已无效");
           }else if(data > 0){
              toRegPlay();
           }else{
             alert("邀请失败");
           }
      });
   }
   
   function logInvited(){
	    if( !checkCookie() ){
	    	alert("请启动cookie");
	    	return;
	    }

		if ($("#userpass").val() == "") {
			alert('用户密码不能为空');
			return;
		}
		
	    var url = "cts/angainAdd?";
  	    url += "userId="+userId_0;
  	    url += "&employeeId="+employeeId_0;
  	    url += "&inviteCode="+invitedCode_0;
  	    url += "&pwd="+$("#userpass").val();
  	    url += "&name="+$("#username").val();
  	    url += "&wtype=2";
		
		$.post(url , null , function(data){
		    if(data == -1){
		       $('#loginmsg').text('邀请已无效');
		    }else if(data == -2){
	       	   $('#loginmsg').text('用户名或密码错');
	        }else if(data == -3){
	       	   $('#loginmsg').text('你已经联通了'+company+'系统');
	       	}else if(data > 1){
	       	   login();
	       	}else{
	       	   alert('邀请失败');
	       	}
		});       	      
   }
   
   $(function(){
       invitedFRAM();
   });
</script>